//
//  DetailContentTableViewCell.m
//  GiftSay
//
//  Created by laohu on 16/1/7.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "DetailContentTableViewCell.h"
#import "HXCatagoryCollectionViewCell.h"
@implementation DetailContentTableViewCell
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"NightModeOpen" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"NightModeOff" object:nil];
    [self createView];
    
    return self;
}
- (void)refresh {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.collectionView.backgroundColor = kNightModeColor;
        self.label.textColor = [UIColor whiteColor];
    } else {
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.label.textColor = [UIColor blackColor];
    }
    [self.collectionView reloadData];
    
}
- (void)layoutSubviews {
    [super layoutSubviews];

    
    self.label.frame = CGRectMake(0, 0, self.width, 50);
    
    self.flowLayout.itemSize = CGSizeMake((self.width - 45) / 4, 130);
    self.collectionView.frame = CGRectMake(5, self.label.bottom + 5, self.width - 10, self.height - self.label.bottom - 5);

}



- (void)setModel:(HXCatagoryFirstModel *)model {

    _model = model;
    self.label.text = model.name;
    [self.collectionView reloadData];
//    NSLog(@"🐱%ld", self.model.channel.count);
    
}

- (void)createView {

    self.label = [[UILabel alloc] initWithFrame:CGRectZero];
    self.label.text = self.model.name;
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.label.textColor = [UIColor whiteColor];
    } else {
        self.label.textColor = [UIColor blackColor];
    }
    self.label.font = [UIFont systemFontOfSize:17 * SCREEN_WIDTH];
    self.label.textAlignment = NSTextAlignmentLeft;
    [self addSubview:_label];

    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.flowLayout.minimumInteritemSpacing = 5;
    self.flowLayout.minimumLineSpacing = 5;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[HXCatagoryCollectionViewCell class] forCellWithReuseIdentifier:@"collectionView"];
//    self.collectionView.backgroundColor = [UIColor whiteColor];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.collectionView.backgroundColor = kNightModeColor;
    } else {
        self.collectionView.backgroundColor = [UIColor whiteColor];
    }
    [self addSubview:_collectionView];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    NSLog(@"🐴%ld", self.model.channel.count);
    return self.model.channel.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HXCatagoryCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView" forIndexPath:indexPath];
    item.model = self.model.channel[indexPath.item];
    
//    item.backgroundColor = [UIColor redColor];
    
    return item;
}
#pragma mark - item 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"JumpToInside" object:self.model.channel[indexPath.item]];
//    NSLog(@"%@", self.model.channel[indexPath.item]);
//    NSLog(@"%@", self.model.channel[indexPath.item]);
//    NSLog(@"不不不不不不不不不");
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
